package com.example.demo.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.demo.config.RetEnum;
import com.example.demo.dao.TeacherDao;
import com.example.demo.entity.Teacher;
import com.example.demo.service.TeacherService;
import com.example.demo.vo.RespInfo;
import com.example.demo.vo.user.TeacherParam;
import com.example.demo.vo.user.TloginParam;
import com.example.demo.vo.user.TloginResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service("teacherService")
@Slf4j
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherDao teacherDao;

    @Override
    public RespInfo queryByTid(Integer tid) {
        Teacher teacher = teacherDao.queryByTid(tid);
        RespInfo<Teacher> respInfo = new RespInfo<>();
        respInfo.setRet(RetEnum.Succeeded);
        respInfo.setData(teacher);
        return respInfo;
    }

    @Override
    public RespInfo insert(TeacherParam param) {
        Teacher teacher = new Teacher();
        teacher.setTid(param.getTid());
        teacher.setTname(param.getTname());
        teacher.setPassword(param.getPassword());
        int num = teacherDao.insert(teacher);
        RespInfo respInfo = new RespInfo<>();
        if (num > 0) {
            respInfo.setRet(RetEnum.Succeeded);
        } else {
            respInfo.setRet(RetEnum.Failed);
        }
        return respInfo;
    }

    @Override
    public RespInfo update(TeacherParam param) {
        Teacher teacher = teacherDao.queryByTid(param.getTid());
        teacher.setTname(param.getTname());
        teacher.setPassword(param.getPassword());
        int num = this.teacherDao.update(teacher);
        RespInfo respInfo = new RespInfo<>();
        if (num > 0) {
            respInfo.setRet(RetEnum.Succeeded);
        } else {
            respInfo.setRet(RetEnum.Failed);
        }
        return respInfo;
    }

    @Override
    public RespInfo deleteByTid(Integer tid) {
        boolean flag = this.teacherDao.deleteByTid(tid)> 0;
        RespInfo respInfo = new RespInfo<>();
        if (flag) {
            respInfo.setRet(RetEnum.Succeeded);
        } else {
            respInfo.setRet(RetEnum.Failed);
        }
        return respInfo;
    }

    @Override
    public RespInfo Tlogin(TloginParam params) {
        Teacher teacher = new Teacher();
        teacher.setTname(params.getTname());
        Teacher value = this.teacherDao.queryAllt(teacher);
        RespInfo<TloginResp> respInfo = new RespInfo<>();
        if(value == null){
            respInfo.setRet(RetEnum.Not_Exit_Teacher);
            return respInfo;
        }

        if(StringUtils.isEmpty(params.getPassword()) || !params.getPassword().equals(value.getPassword())){
        respInfo.setRet(RetEnum.Teacher_Password_Error);
        return respInfo;
        }
        TloginResp tloginResp = new TloginResp();
        tloginResp.setTname(value.getTname());
        respInfo.setRet(RetEnum.Succeeded);
        respInfo.setData(tloginResp);
        return respInfo;
    }


}
